package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.CompanyInfoEntity;
import com.cheng.data.mapper.CompanyInfoMapper;
import com.cheng.data.vo.CompanyInfoVO;
import com.cheng.data.vo.StatisticsVO;
import com.cheng.operation.GvmtTypeInfOperation;
import com.cheng.service.CompanyInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/11/29 21:12
 * @version: 1.0
 * @description:
 */
@Service
public class CompanyInfoServiceImpl implements CompanyInfoService {

    private final static Logger log = LoggerFactory.getLogger(CompanyInfoServiceImpl.class);

    @Autowired
    private CompanyInfoMapper companyInfoMapper;

    @Autowired
    private GvmtTypeInfOperation gvmtTypeInfOperation;

    /**
    * @author: ChengJW
    * @params: [companyInfoEntity]
    * @return: void
    * @date: 2021/11/29
    * @description: 插入 company_info
    */
    @Override
    public void insertCompanyInfo(CompanyInfoEntity companyInfoEntity) throws CommonException {
        companyInfoMapper.insert(companyInfoEntity);
    }

    /**
    * @author: ChengJW
    * @params: [map]
    * @return: java.util.List<com.cheng.data.vo.CompanyInfoVO>
    * @date: 2021/11/29
    * @description: 查询企业信息
    */
    @Override
    public List<CompanyInfoVO> queryCompanyVOList(Map<String, Object> map) throws CommonException{
        return companyInfoMapper.queryCompanyVOList(map);
    }

    /**
    * @author: ChengJW
    * @params: [companyInfoEntity]
    * @return: void
    * @date: 2021/11/30
    * @description: 删除企业信息
    */
    @Override
    public void deleteCompanyInfo(CompanyInfoEntity companyInfoEntity) throws CommonException {

        QueryWrapper<CompanyInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(companyInfoEntity.getCiBussId()),"ci_Buss_Id",companyInfoEntity.getCiBussId());
        wrapper.eq(StringUtil.isNotNull(companyInfoEntity.getCiId()),"ci_Id",companyInfoEntity.getCiId());
        companyInfoMapper.delete(wrapper);
    }

    /**
    * @author: ChengJW
    * @params: [companyInfoEntity]
    * @return: void
    * @date: 2021/12/1
    * @description: 修改企业信息
    */
    @Override
    public void updateCompanyInfo(CompanyInfoVO vo) throws CommonException {
        QueryWrapper<CompanyInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(vo.getCiId()),"ci_Id",vo.getCiId());
        wrapper.eq(StringUtil.isNotNull(vo.getCiBussId()),"ci_Buss_Id",vo.getCiBussId());
        wrapper.eq(StringUtil.isNotNull(vo.getCiCreditCode()),"ci_Credit_Code",vo.getCiCreditCode());
        //wrapper.eq(StringUtil.isNotNull(vo.getCiMgeCode()),"ci_Mge_Code",vo.getCiMgeCode());
        List<CompanyInfoEntity> list = companyInfoMapper.selectList(wrapper);
        CompanyInfoEntity entity = null;
        if (list != null && list.size() > 0){
            //修改受理信息的档案页数时，一同修改企业信息的档案页数
            if (vo.getCiAresTolNum() > 0) {
                entity = list.get(0);
                entity.setCiAresTolNum(vo.getCiAresTolNum());
                entity.setUtDt(new Date());
                companyInfoMapper.updateById(entity);
            }
            //档案迁出、迁入时，批量修改企业的管辖机关代码跟管辖机关名称
            if(StringUtil.isNotNull(vo.getCiMgeCode())){
                for (CompanyInfoEntity entity1 : list){
                    entity1.setCiMgeCode(vo.getCiMgeCode());
                    if (CodeUtil.MOVE_STD_1.equals(vo.getCiMvCd())){
                        entity1.setCiMvCd(CodeUtil.MOVE_STD_0);
                    }else {
                        entity1.setCiMvCd(CodeUtil.MOVE_STD_1);
                    }
                    entity1.setCiMgeNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(vo.getCiMgeCode()));
                    entity1.setUtDt(new Date());
                    companyInfoMapper.updateById(entity1);
                }
            }
        }
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: void
    * @date: 2021/12/20
    * @description: 修改企业信息
    */
    @Override
    public void updateCompanyInfo(CompanyInfoEntity entity) throws CommonException {
        companyInfoMapper.updateById(entity);
    }

    /**
    * @author: ChengJW
    * @params: [companyInfoVO]
    * @return: com.cheng.data.entity.CompanyInfoEntity
    * @date: 2021/12/20
    * @description: 查询企业信息
    */
    @Override
    public CompanyInfoEntity queryCompanyInfoList(CompanyInfoVO vo) throws CommonException {
        QueryWrapper<CompanyInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(vo.getCiId()),"ci_Id",vo.getCiId());
        wrapper.eq(StringUtil.isNotNull(vo.getCiBussId()),"ci_Buss_Id",vo.getCiBussId());
        List<CompanyInfoEntity> list = companyInfoMapper.selectList(wrapper);
        if (list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    /**
    * @author: ChengJW
    * @params: [map]
    * @return: java.util.List<com.cheng.data.vo.CompanyInfoVO>
    * @date: 2021/12/15
    * @description: 档案迁出列表查询
    */
    @Override
    public List<CompanyInfoVO> queryArchivesMoveOutList(Map<String, Object> map) throws CommonException {
        return companyInfoMapper.queryArchivesMoveOutList(map);
    }

    /**
    * @author: ChengJW
    * @params: [vo]
    * @return: java.util.List<com.cheng.data.vo.CompanyInfoVO>
    * @date: 2021/12/17
    * @description: 根据条件查询企业信息
    */
    @Override
    public List<CompanyInfoEntity> queryCompanyInfoList(Map<String,Object> map) throws CommonException {

        QueryWrapper<CompanyInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull((String) map.get("ci_Buss_Id")),"ci_Buss_Id",(String) map.get("ci_Buss_Id"));
        wrapper.eq(StringUtil.isNotNull((String) map.get("ci_Id")),"ci_Id",(String) map.get("ci_Id"));
        wrapper.eq(StringUtil.isNotNull((String) map.get("ci_Credit_Code")),"ci_Credit_Code",(String) map.get("ci_Credit_Code"));
        wrapper.eq(StringUtil.isNotNull((String) map.get("ci_Mge_Code")),"ci_Mge_Code",(String) map.get("ci_Mge_Code"));
        return companyInfoMapper.selectList(wrapper);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: java.util.List<com.cheng.data.vo.StatisticsVO>
     * @date: 2021/12/19
     * @description: 根据企业类型，统计企业信息数据
     */
    @Override
    public List<StatisticsVO> queryStatisticsVOlist(Map<String, Object> map) throws CommonException {
        if (map == null){
            return null;
        }
        if (CodeUtil.STATISTIC_TYPE.equals(map.get(CodeUtil.STATISTIC_CODE))){
            return companyInfoMapper.queryStatistcsQyList(map);
        }
        return companyInfoMapper.queryStatistcsQyListByCode(map);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: java.util.List<com.cheng.data.vo.StatisticsVO>
     * @date: 2021/12/20
     * @description: 根据档案类型，统计档案信息数据
     */
    @Override
    public List<StatisticsVO> queryStatisticsArchVOList(Map<String, Object> map) throws CommonException {
        if (map == null){
            return null;
        }
        if (CodeUtil.STATISTIC_TYPE.equals(map.get(CodeUtil.STATISTIC_CODE))){
            return companyInfoMapper.queryStatisticsArchList(map);
        }
        return companyInfoMapper.queryStatisticsArchListByCode(map);
    }
}
